package JwtDemo;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.util.*;

public class JwtDemo extends JwtAccessTokenConverter {

    public static void main(String[] args) {
        JwtDemo demo = new JwtDemo();
        String signingKey = "your-signing-key"; // 请替换为你的签名密钥
        demo.setSigningKey(signingKey);

        // 创建用户权限
        Set<SimpleGrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

        // 创建 OAuth2Request
        OAuth2Request request = new OAuth2Request(null, "client_id",
                authorities, true, null, null, null, null, null);

        // 创建 Authentication
        Authentication authentication = new TestingAuthenticationToken("user", "password", authorities);

        // 创建 OAuth2Authentication
        OAuth2Authentication oauth2Authentication = new OAuth2Authentication(request, authentication);

        // 创建 OAuth2AccessToken 实例
        DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("your-jwt-token");
        Map<String, Object> additionalInformation = new HashMap<>();
        additionalInformation.put("user_id", "12345"); // 添加用户信息
        additionalInformation.put("user_name", "user"); // 添加用户名
        additionalInformation.put("authorities", authorities); // 添加权限
        token.setAdditionalInformation(additionalInformation);

        // 使用 JwtDemo 生成 JWT
        String jwt = demo.encode(token, oauth2Authentication);
        System.out.println("生成的 JWT: " + jwt);

        String[] chunks = jwt.split("\\.");
        Base64.Decoder decoder = Base64.getUrlDecoder();

        try {
            String header = new String(decoder.decode(chunks[0]));
            String payload = new String(decoder.decode(chunks[1]));
            System.out.println("Header: " + header);
            System.out.println("Payload: " + payload);
        } catch (IllegalArgumentException e) {
            System.err.println("解码时出错: " + e.getMessage());
        }
    }
}

// 自定义的 Authentication 实现
class TestingAuthenticationToken extends org.springframework.security.authentication.UsernamePasswordAuthenticationToken {
    public TestingAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        super(principal, credentials, authorities);
    }
}